package com.doge.calcalbum.business.vip

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.core.view.isVisible
import com.doge.calcalbum.R
import com.doge.calcalbum.databinding.DialogVipInfo2Binding
import com.doge.calcalbum.utils.disableSkin
import com.doge.calcalbum.utils.log
import com.doge.calcalbum.utils.setOnClickCallback

class VipInfoDialog private constructor(context: Context, theme: Int) : Dialog(context, theme) {
    private class Params {
        lateinit var mContext: Context
        var canCancel = false
        lateinit var binding: DialogVipInfo2Binding
        var confirmText: Int? = null
        var confirmCallback: DialogInterface.OnClickListener? = null
        var cancelCallback: DialogInterface.OnClickListener? = null
    }

    companion object {
        var isDisplaying = false
    }

    override fun show() {
        if (isDisplaying) return
        super.show()
        isDisplaying = true
    }

    override fun dismiss() {
        super.dismiss()
        isDisplaying = false
    }


    class Builder(context: Context) {
        private val p = Params()

        init {
            p.mContext = context
        }

        fun show() {
            create().show()
        }

        fun setConfirmText(res: Int): Builder {
            p.confirmText = res
            return this
        }

        fun setCallback(
            callback: DialogInterface.OnClickListener,
            cancelCallback: DialogInterface.OnClickListener,
        ): Builder {
            p.confirmCallback = callback
            p.cancelCallback = cancelCallback
            return this
        }

        fun create(): VipInfoDialog {
            val dialog = VipInfoDialog(
                p.mContext,
                R.style.Theme_Light_NoTitle_Dialog
            )

            dialog.window?.decorView?.setPadding(0, 0, 0, 0)
            val lp = dialog.window?.attributes
            lp?.width = WindowManager.LayoutParams.MATCH_PARENT
            lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
            dialog.window?.attributes = lp

            p.binding = DialogVipInfo2Binding.inflate(LayoutInflater.from(p.mContext))
            p.confirmText?.let { p.binding.confirm.setText(it) }
            p.binding.confirm.setOnClickCallback {
                dialog.dismiss()
                p.confirmCallback?.onClick(dialog, 0)
            }
            p.binding.close.setOnClickListener {
                dialog.dismiss()
                p.cancelCallback?.onClick(dialog,-1)
            }

            val skinViews = arrayOf(
                    p.binding.tip2Image,
                    p.binding.tip2Text,
            )
            skinViews.forEach { it.isVisible = !disableSkin }

            dialog.setContentView(p.binding.root)
            dialog.setCanceledOnTouchOutside(p.canCancel)
            dialog.setCancelable(p.canCancel)
            return dialog
        }
    }
}